package com.xinzhi.savingpot.util;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;


public class JwtUtil {
    public static final String SUBJECT = "xinzhi";

    public static final long EXPIRE = 1000 * 60 * 500;  //过期时间，毫秒，一秒


    //秘钥
    public static final String APPSECRET = "c60b98a542615d7d26e3724f26356a47";

    /**
     * 生成jwt
     */
    public static String createJsonWebToken(Integer userID,String userName) {

        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("userId", userID)
                .claim("userName", userName)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .signWith(SignatureAlgorithm.HS256, APPSECRET).compact();
        return token;
    }


    /**
     * 校验token
     */
    public static Claims checkJWT(String token) {

        try {
            final Claims claims = Jwts.parser().setSigningKey(APPSECRET).
                    parseClaimsJws(token).getBody();
            return claims;

        } catch (Exception e) {
        }
        return null;


    }
}
